package com.yeyks.controller.financial;

import com.yeyks.account.dal.domain.AccountInfo;
import com.yeyks.annotation.AdminAppLog;
import com.yeyks.common.appcommon.dto.account.AppAccountDTO;
import com.yeyks.common.appcommon.param.finance.AddWithdrawalParam;
import com.yeyks.common.appcommon.param.finance.QueryWithdrawalOrderParam;
import com.yeyks.common.appcommon.result.CommonPage;
import com.yeyks.common.appcommon.result.CommonResult;
import com.yeyks.common.constants.AccountType;
import com.yeyks.common.exception.OftenFiledException;
import com.yeyks.common.exception.ServiceException;
import com.yeyks.common.param.finance.FinanceWithdrawalParam;
import com.yeyks.common.result.withdrawalorder.HotelWithdrawalOrderResult;
import com.yeyks.common.utils.apputil.bean.MyBeanUtils;
import com.yeyks.commonReference.service.hotel.HotelWithdrawalOrderService;
import com.yeyks.controller.BaseController;
import com.yeyks.hotel.dal.domain.HotelWithdrawalOrder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;


/**
 * 财务c2b酒店模块接口
 *
 * @author Administrator
 */
@Slf4j
@Api(tags = "AppFinanceC2bHotelController财务c2b酒店模块接口", description = "财务c2b酒店模块接口[查询分页订单列表][酒店申请提现]")
@RestController
@RequestMapping("/c2b/hotel/finance")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class AppFinanceC2bHotelController extends BaseController {

    private final HotelWithdrawalOrderService hotelWithdrawalOrderService;

    @ApiOperation(value = "酒店提现订单查询,[财务结算的列表]")
    @PostMapping("/list")
    public CommonResult<CommonPage<HotelWithdrawalOrderResult>> list(@Valid QueryWithdrawalOrderParam param) {

        AppAccountDTO appAccountDTO = validToken(param.getToken(), param.getVersion(), param.getPlatform());

        param.setOrderType(HotelWithdrawalOrder.OrderType.ORDER.value());
        return hotelWithdrawalOrderService.adminAppList(param, MyBeanUtils.convert(appAccountDTO, AccountInfo.class));
    }

    @AdminAppLog("酒店申请提现")
    @ApiOperation(value = "酒店申请提现", notes = "酒店申请提现")
    @PostMapping("/add/withdrawal")
    public CommonResult<Boolean> add(@Valid AddWithdrawalParam param) {

        AppAccountDTO appAccountDTO = validToken(param.getToken(),
                param.getVersion(), param.getPlatform());

        if (AccountType.HotelManager != appAccountDTO.getType()) {
            return CommonResult.failed("当前账户无权提现");
        }
        try {
            param.setType(HotelWithdrawalOrder.OrderType.ORDER.value());
            if (!hotelWithdrawalOrderService.withdrawal(MyBeanUtils.convert(appAccountDTO, AccountInfo.class),
                    MyBeanUtils.convert(param, FinanceWithdrawalParam.class))) {
                return CommonResult.failed("新增提现订单异常");
            }
            return CommonResult.success(true);
        } catch (ServiceException e) {
            throw new OftenFiledException(e.getMessage());
        }
    }

}
